package top.hymsk.ecommerce.controller.restful.product;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.hymsk.ecommerce.anno.Log;
import top.hymsk.ecommerce.anno.TokenAnalysis;
import top.hymsk.ecommerce.pojo.response.GeneralResponse;
import top.hymsk.ecommerce.pojo.status.ProductStatusCode;
import top.hymsk.ecommerce.pojo.status.ResponseStatusCode;
import top.hymsk.ecommerce.service.ProductService;

import javax.servlet.http.HttpServletRequest;

/**
 * 卖家商品 /product/seller
 *
 * @author 花于陌上开
 */
@CrossOrigin  //允许跨域访问
@RestController
@RequestMapping("/product/seller")
public class SellerProductRestController {
    private final HttpServletRequest request;
    private final ProductService productService;

    public SellerProductRestController(HttpServletRequest request, ProductService productService) {
        this.request = request;
        this.productService = productService;
    }

    /**
     * 获取商品信息
     * (基本完成)
     *
     * @param token     用户鉴权token
     * @param productId 商品id
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/get")
    public GeneralResponse get(String token, Integer productId) {
        Integer storeId = (Integer) request.getAttribute("storeId");
        if (storeId == null) {
            return GeneralResponse.fail(ResponseStatusCode.NO_ACCESS);
        }
        return productService.getProductBySeller(productId);
    }

    /**
     * 获取商品信息列表
     * (基本完成)
     *
     * @param token 用户鉴权token
     * @param page  分页参数
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/list")
    public GeneralResponse list(String token, Integer page) {
        Integer storeId = (Integer) request.getAttribute("storeId");
        if (storeId == null) {
            return GeneralResponse.fail(ResponseStatusCode.NO_ACCESS);
        }
        return productService.getProductManageList(storeId, page);
    }


    /**
     * 上传商品，同步上传ES
     * (基本完成)
     *
     * @param token   用户鉴权token
     * @param title   商品标题
     * @param price   商品价格
     * @param picture 图片 @RequestParam("picture")
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/upload")
    public GeneralResponse upload(String token, String title, Double price, MultipartFile picture) {
        Integer storeId = (Integer) request.getAttribute("storeId");
        if (storeId == null) {
            return GeneralResponse.fail(ResponseStatusCode.NO_ACCESS);
        }
        return productService.upload(storeId, title, price, picture);
    }

    /**
     * 停售商品，同步删除ES
     * (基本完成)
     *
     * @param token     用户鉴权token
     * @param productId 商品id
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/delete")
    public GeneralResponse delete(String token, Integer productId) {
        Integer storeId = (Integer) request.getAttribute("storeId");
        if (storeId == null) {
            return GeneralResponse.fail(ResponseStatusCode.NO_ACCESS);
        }
        return productService.updateStatus(storeId, productId, ProductStatusCode.OUT_OF_SALE);
    }

    /**
     * 重新售卖商品，同步更新ES
     * (基本完成)
     *
     * @param token     用户鉴权token
     * @param productId 商品id
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/reshelve")
    public GeneralResponse reshelve(String token, Integer productId) {
        Integer storeId = (Integer) request.getAttribute("storeId");
        if (storeId == null) {
            return GeneralResponse.fail(ResponseStatusCode.NO_ACCESS);
        }
        return productService.updateStatus(storeId, productId, ProductStatusCode.ON_SALE);
    }

    /**
     * 为商品开通VIP，同步更新ES
     * (基本完成)
     *
     * @param token     用户鉴权token
     * @param productId 商品id
     * @param level     VIP等级
     *                  [2]普通VIP 权重+0.5 2000元/件/月
     *                  [3]超级VIP 权重+1.0 5000元/件/月
     *                  [4]至尊VIP 权重+1.5 10000元/件/月
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/vip")
    public GeneralResponse openVip(String token, Integer productId, Integer level) {
        ProductStatusCode statusCode;
        if (level == 2) {
            statusCode = ProductStatusCode.ON_SALE_VIP;
        } else if (level == 3) {
            statusCode = ProductStatusCode.ON_SALE_SUPER_VIP;
        } else if (level == 4) {
            statusCode = ProductStatusCode.ON_SALE_SUPREMACY_VIP;
        } else {
            return GeneralResponse.fail(ResponseStatusCode.REQUEST_ERROR);
        }
        Integer storeId = (Integer) request.getAttribute("storeId");
        if (storeId == null) {
            return GeneralResponse.fail(ResponseStatusCode.NO_ACCESS);
        }
        return productService.updateStatus(storeId, productId, statusCode);
    }

    /**
     * 修改商品信息，同步修改ES
     * (基本完成)
     *
     * @param token     用户鉴权token
     * @param productId 商品id
     * @param title     商品标题
     * @param price     商品价格
     * @param picture   图片
     * @return 响应信息
     */
    @Log
    @TokenAnalysis
    @PostMapping("/update")
    public GeneralResponse update(String token, Integer productId, String title, Double price, MultipartFile picture) {
        Integer storeId = (Integer) request.getAttribute("storeId");
        if (storeId == null) {
            return GeneralResponse.fail(ResponseStatusCode.NO_ACCESS);
        }
        return productService.update(storeId, productId, title, price, picture);
    }


}
